﻿Imports Telerik.Web.UI
Imports System.Data

Partial Class Budget
    Inherits System.Web.UI.Page

    Dim _CommandName As String

    Dim BudgetObj As New DBBudgetSP
    Dim BudgetDatatset As New Data.DataSet

    Dim BudgetAccountsObj As New DBBudgetAccountsSP
    Dim BudgetAccountsDatatset As New Data.DataSet

    Public strConnection As String = ConfigurationManager.ConnectionStrings("DatabaseConnectionString").ConnectionString.ToString

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim _lblPageTitle As Label
            _lblPageTitle = Me.Master.FindControl("lblPageTitle")
            _lblPageTitle.Text = GetLocalResourceObject("PageTitle")
            rntbYear.Text = Now.Year
            'BindGrid()
            BudgetDatatset = BudgetObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvSelectedBudget", , rntbYear.Text, , )
            If BudgetDatatset.Tables.Count > 0 Then
                If BudgetDatatset.Tables(0).Rows.Count > 0 Then
                    hdnBudgetID.Value = BudgetDatatset.Tables(0).Rows(0).Item("ID").ToString()
                Else
                    hdnBudgetID.Value = ""
                End If
            Else
                hdnBudgetID.Value = ""
            End If
            odsBudgetAccounts.SelectParameters("BudgetID").DefaultValue = hdnBudgetID.Value
            rgBudgetAccounts.Rebind()
        End If

    End Sub

    Protected Sub rgBudgetAccounts_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles rgBudgetAccounts.PreRender
            For Each item As GridItem In rgBudgetAccounts.MasterTableView.Items
                If TypeOf item Is GridEditableItem Then
                    Dim editableItem As GridEditableItem = TryCast(item, GridDataItem)
                    editableItem.Edit = True
                End If
            Next
            rgBudgetAccounts.Rebind()
    End Sub

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Me.Theme = GetGlobalResourceObject("SystemResource", "Theme")
    End Sub


    'Private Sub BindGrid()
    '    Dim GridLines As RadGrid = rgBudgetAccounts
    '    'Declare a datatable for the gridview

    '    Dim dt As New DataTable()
    '    'Add Columns to the datatable
    '    BudgetDatatset = BudgetObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvSelectedBudget", , rntbYear.Text, , )

    '    If BudgetDatatset.Tables(0).Rows.Count = 0 Then
    '        BudgetAccountsDatatset = BudgetAccountsObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvBudgetAccountsByBudgetID", , 0, , , , , , , , )
    '    Else
    '        BudgetAccountsDatatset = BudgetAccountsObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvBudgetAccountsByBudgetID", , BudgetDatatset.Tables(0).Rows(0).Item("ID").ToString(), , , , , , , , )
    '    End If
    '    'Now bind the datatable to RadGrid
    '    dt = BudgetAccountsDatatset.Tables(0)

    '    'Add 3 Calculation Columns ("BudgetTotal","SpentAmount","LeftAmount")
    '    Dim dc As DataColumn = dt.Columns.Add("Total", GetType([Double]))
    '    Dim dc2 As DataColumn = dt.Columns.Add("SpentAmount", GetType([Double]))
    '    Dim dc3 As DataColumn = dt.Columns.Add("LeftAmount", GetType([Double]))

    '    'Assign The Values to the Columns
    '    For Each dr As DataRow In dt.Rows
    '        Dim amount As Double
    '        Dim IncreaseAmount As Double
    '        Double.TryParse(IIf(dr("Amount") Is DBNull.Value, "", dr("Amount")), amount)
    '        Double.TryParse(IIf(dr("IncreaseAmount") Is DBNull.Value, "", dr("IncreaseAmount")), IncreaseAmount)
    '        dr("Total") = amount + IncreaseAmount
    '        dr("SpentAmount") = (amount + IncreaseAmount) - amount
    '        dr("LeftAmount") = amount - ((amount + IncreaseAmount) - amount)
    '    Next

    '    'Define DataSource
    '    GridLines.DataSource = dt
    '    GridLines.DataBind()
    '    'Store the datatable in the viewstate
    '    ViewState("Data") = dt

    'End Sub

    Protected Sub rgBudgetAccounts_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) Handles rgBudgetAccounts.ItemDataBound
        If TypeOf e.Item Is GridDataItem AndAlso e.Item.IsInEditMode Then
            Dim cancelButton As ImageButton = DirectCast(e.Item.FindControl("CancelButton"), ImageButton)
            cancelButton.Visible = False
            Dim updateButton As ImageButton = DirectCast(e.Item.FindControl("UpdateButton"), ImageButton)
            updateButton.Visible = False
        End If
    End Sub

    'Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridCommandEventArgs) Handles rgBudgetAccounts.EditCommand
    '    'Change the gridview to edit mode
    '    rgBudgetAccounts.EditIndexes.Add(e.Item.ItemIndex)
    '    e.Item.Edit = True
    '    'rgVoucherLines.MasterTableView.
    '    'Now bind the gridview
    '    rgBudgetAccounts.DataSource = DirectCast(ViewState("Data"), DataTable)
    '    rgBudgetAccounts.Rebind()
    'End Sub
    'Protected Sub rgVoucherLines_RowUpdating(ByVal sender As Object, ByVal e As GridCommandEventArgs) Handles rgBudgetAccounts.UpdateCommand

    '    If (TypeOf e.Item Is GridDataItem AndAlso e.Item.IsInEditMode) Then
    '        'If (TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode) Then

    '        'Dim editFormItem As GridEditFormItem = CType(e.Item, GridEditFormItem)
    '        'Dim parentItem As GridDataItem = editFormItem.ParentItem

    '        Dim editItem As GridEditableItem = CType(e.Item, GridEditableItem)
    '        Dim Amount As Integer = CType(editItem("Amount").Controls(0), RadNumericTextBox).Text

    '        Dim dr As DataRow = DirectCast(ViewState("Data"), DataTable).Rows(e.Item.ItemIndex)

    '        dr.BeginEdit()
    '        dr("Amount") = Amount
    '        dr.EndEdit()
    '        dr.AcceptChanges()

    '        'close the edit form
    '        rgBudgetAccounts.MasterTableView.ClearEditItems()
    '        e.Canceled() = True

    '        'Now bind the datatable to the RadGrid
    '        rgBudgetAccounts.DataSource = DirectCast(ViewState("Data"), DataTable)
    '        rgBudgetAccounts.DataBind()

    '    End If

    'End Sub


    'Protected Sub rgUsers_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgBudgetAccounts.ItemCommand
    '    _CommandName = e.CommandName

    '    If _CommandName = "RebindGrid" Then

    '        rgBudgetAccounts.DataSource = DirectCast(ViewState("Data"), DataTable)
    '        rgBudgetAccounts.DataBind()

    '    End If

    '    If _CommandName = "Cancel" Then
    '        rgBudgetAccounts.MasterTableView.ClearEditItems()
    '        rgBudgetAccounts.MasterTableView.IsItemInserted = False
    '        e.Canceled() = True
    '        rgBudgetAccounts.DataSource = DirectCast(ViewState("Data"), DataTable)
    '        rgBudgetAccounts.DataBind()

    '    End If

    '    End Sub


    Protected Sub Year_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        'DirectCast(ViewState("Data"), DataTable).Rows.Clear()
        'rgBudgetAccounts.DataBind()

        BudgetDatatset = BudgetObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvSelectedBudget", , rntbYear.Text, , )

        If BudgetDatatset IsNot Nothing Then
            If BudgetDatatset.Tables(0).Rows.Count > 0 Then
                rtbPrgram.Text = BudgetDatatset.Tables(0).Rows(0).Item("Program").ToString()
                rtbActivityNum.Text = BudgetDatatset.Tables(0).Rows(0).Item("ActivityNum").ToString()
                hdnBudgetID.Value = BudgetDatatset.Tables(0).Rows(0).Item("ID").ToString()
                odsBudgetAccounts.SelectParameters("BudgetID").DefaultValue = hdnBudgetID.Value
                rgBudgetAccounts.Rebind()
                'BindGrid()
            Else
                hdnBudgetID.Value = ""
                odsBudgetAccounts.SelectParameters("BudgetID").DefaultValue = hdnBudgetID.Value
                rgBudgetAccounts.Rebind()
                'BindGrid()
            End If
        End If

    End Sub



    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            SaveControls()
            BudgetDatatset = BudgetObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "RtvSelectedBudget", , rntbYear.Text, , )
            If BudgetDatatset.Tables.Count > 0 Then
                If BudgetDatatset.Tables(0).Rows.Count > 0 Then
                    hdnBudgetID.Value = BudgetDatatset.Tables(0).Rows(0).Item("ID").ToString()
                Else
                    hdnBudgetID.Value = ""
                End If
            Else
                hdnBudgetID.Value = ""
            End If
            odsBudgetAccounts.SelectParameters("BudgetID").DefaultValue = hdnBudgetID.Value
            rgBudgetAccounts.Rebind()
            'BindGrid()
            RadAjaxManager1.ResponseScripts.Add("radalert('Information Saved Successfully !', 270, 100);")
        Catch ex As Exception
            RadAjaxManager1.ResponseScripts.Add("radalert('An Unknown ERROR Occurred ... Try Again Later ', 270, 100);")
        End Try

        'Dim radalertscript As String = "radalert('Voucher Saved Successfully !', 270, 100);"
        'Page.ClientScript.RegisterStartupScript(Me.GetType(), "radalert()", radalertscript)
        'Exit Sub

    End Sub


    Sub SaveControls()

        ''Grid = rgBudgetAccounts
        'Dim MyID As String
        'Dim Operation As String
        'Dim DeleteFlag As Boolean = False

        'BudgetDatatset = BudgetObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "SaveBudget", , rntbYear.Text, rtbPrgram.Text, rtbActivityNum.Text)

        'If BudgetDatatset.Tables(0).Rows.Count > 0 Then
        '    MyID = BudgetDatatset.Tables(0).Rows(0).Item("ID").ToString()
        '    Operation = BudgetDatatset.Tables(0).Rows(0).Item("Operation").ToString()
        'End If

        'If Operation = "Update" Then
        '    BudgetAccountsDatatset = BudgetAccountsObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "DeleteBudgetAccountsByBudgetID", , MyID, , , , , , , )
        '    DeleteFlag = True
        'End If

        'If rgBudgetAccounts.MasterTableView.Items.Count > 0 Then

        '    For Each DataItem As GridDataItem In rgBudgetAccounts.Items

        '        Dim ParentItem As GridDataItem = DataItem.OwnerTableView.ParentItem

        '        Dim Amount As String = CType(DataItem("Amount").Controls(0), RadNumericTextBox).Text.ToString()
        '        Dim AccountNumber As String = CType(DataItem("AccountNumber").Controls(0), TextBox).Text.ToString()

        '        If Not (Amount = "" Or Amount Is DBNull.Value Or Amount = "&nbsp;") Then
        '            If Amount.Contains(",") Then
        '                Amount = Amount.Replace(",", "")
        '            End If
        '            'If Amount.Contains(".") Then
        '            'Amount = Amount.Replace(".", "")
        '            'End If
        '            'Send Values to Database
        '            BudgetAccountsDatatset = BudgetAccountsObj.QueryDatabase(DBParentStoredProcedureClass.SqlQueryType.FillDataset, "SaveBudgetAccounts", , MyID, Amount, , , , AccountNumber, , )
        '        End If

        '    Next

        'End If

    End Sub


End Class
